package cn.starlightsoftware.sherly.db.mapper.permission;

import cn.starlightsoftware.sherly.controller.admin.permission.vo.PermissionPageReqVO;
import cn.starlightsoftware.sherly.db.model.permission.PermissionDO;
import cn.starlightsoftware.sherly.framework.mybatisplus.mapper.SherlyMapper;
import cn.starlightsoftware.sherly.framework.mybatisplus.wrapper.SherlyLambdaQueryWrapper;
import cn.starlightsoftware.sherly.model.PageResult;

import java.util.List;

/**
 * @author 谷子毅
 */
public interface PermissionMapper extends SherlyMapper<PermissionDO> {
    default PageResult<PermissionDO> selectPage(PermissionPageReqVO reqVO) {
        return selectPage(reqVO, PermissionDO.class, new SherlyLambdaQueryWrapper<PermissionDO>()
                .eqIfExist(PermissionDO::getPermissionCode, reqVO.getPermissionCode())
                .eqIfExist(PermissionDO::getPermissionName, reqVO.getPermissionName())
                .eqIfExist(PermissionDO::getPermissionType, reqVO.getPermissionType())
                .eqIfExist(PermissionDO::getComponentName, reqVO.getComponentName())
                .eqIfExist(PermissionDO::getComponentPath, reqVO.getComponentPath())
                .eqIfExist(PermissionDO::getPath, reqVO.getPath())
                .eqIfExist(PermissionDO::getUsable, reqVO.getUsable())
                .eqIfExist(PermissionDO::getSort, reqVO.getSort())
                .eqIfExist(PermissionDO::getParentId, reqVO.getParentId())
                .eqIfExist(PermissionDO::getVisible, reqVO.getVisible())
                .eqIfExist(PermissionDO::getAlwaysShow, reqVO.getAlwaysShow())
                .eqIfExist(PermissionDO::getKeepAlive, reqVO.getKeepAlive()));
    }

    default List<PermissionDO> selectTopPermissions() {
        return selectList(new SherlyLambdaQueryWrapper<PermissionDO>()
                .isNull(PermissionDO::getParentId));
    }
}
